<?php

namespace app\common\lib;

use think\facade\Db;

class Eyoucms
{
    public function __construct($web){
        $this->web = $web;
        if(empty($this->web)){
            return null;
        }
    }

    public function insertProject($param){
        $table = 'archives';
        $mod = Db::connect('dynamic')->name('channeltype')->field('id,ntitle,table')->find($this->web['cms_project']);
        $tableExt = $mod['table'].'_content';
        $typeid = Db::connect('dynamic')->name('arctype')->where([
            ['channeltype','=',$mod['id']],
            ['grade','=',2],
            ['typename','=',$param['xiaolei']]
        ])->value('id');
        if(empty($typeid)){
            return null;
        }
        $time = time();
        $data = [
            'typeid'      => $typeid,
            'channel'     => $mod['id'],
            'title'       => $param['title'],
            'admin_id'    => 1,
            'sort_order'  => 100,
            'arcrank'     => -2,
            'click'       => mt_rand(100,999),
            'author'      => '小编',
            'add_time'    => $time,
        ];
        $arr = [1.5,1.8,2.2,2.5,3,3.2];
        $data['follow'] = intval($arr[array_rand($arr)] * $data['click']);

        $insertId = Db::connect('dynamic')->name($table)->insertGetId($data);
        if(empty($insertId)){
            return null;
        }
        
        $pattern_arr = ['连锁','直营','特许','代理','其他'];
        $parrern_keys = array_rand($pattern_arr,mt_rand(2,3));
        $pattern = '';
        foreach ($parrern_keys as $v) {
            $pattern .= $pattern_arr[$v].',';
        }
        $pattern = rtrim($pattern,',');

        $crowd_arr = ['大学生创业','90后创业','00后创业','女性创业','夫妻创业','退伍军人','白领创业','副业创收','小白创业','合伙创业','个人开店','零经验开店'];
        $crowd_keys = array_rand($pattern_arr,mt_rand(2,6));
        $crowd = '';
        foreach ($crowd_keys as $v) {
            $crowd .= $crowd_arr[$v].',';
        }
        $crowd = rtrim($crowd,',');

        $dataExt = [
            'aid'         => $insertId,
            'add_time'    => $time,
            'update_time' => $time,
            'cost_min'    => $param['cost_min'],
            'cost_max'    => $param['cost_max'],
            'business'    => $param['business'],
            'origin'      => $param['origin'],
            'slogan'      => $param['slogan'],
            'shop_num'    => $param['shop_num'],
            'tiaojian'    => $param['tiaojian'],
            'youshi'      => $param['youshi'],
            'liucheng'    => $param['liucheng'],
            'zhichi'      => $param['zhichi'],
            'js_dpmj'     => $param['initData']['dpmj'],
            'js_fzfy'     => $param['initData']['fzfy'],
            'js_cdzx'     => $param['initData']['cdzx'],
            'js_sbfy'     => $param['initData']['sbfy'],
            'js_cpwl'     => $param['initData']['cpwl'],
            'js_rygz'     => $param['initData']['rygz'],
            'js_sdzf'     => $param['initData']['sdzf'],
            'js_xcfy'     => $param['initData']['xcfy'],
            'js_rjxf'     => $param['initData']['rjxf'],
            'js_rkll'     => $param['initData']['rkll'],
            'js_mll'      => $param['initData']['mll'],
            'js_ldzj'     => $param['initData']['ldzj'],
            'js_total'    => $param['initData']['total'],
            'feiyong'     => $param['feiyong'],
            'lirun'       => $param['lirun'],
            'company'     => $param['company'],
            'pattern'     => $pattern,
            'crowd'       => $crowd,
        ];
        $param['cost_max'] = intval($param['cost_max']);
        if($param['cost_max'] > 20){
            $dataExt['cost'] = '20-50万';
        }elseif($param['cost_max'] > 10 && $param['cost_max'] <= 20){
            $dataExt['cost'] = '10-20万';
        }elseif($param['cost_max'] > 5 && $param['cost_max'] <= 10){
            $dataExt['cost'] = '5-10万';
        }elseif($param['cost_max'] > 3 && $param['cost_max'] <= 5){
            $dataExt['cost'] = '3-5万';
        }else{
            $dataExt['cost'] = '1-3万';
        }

        $count = Db::connect('dynamic')->name($tableExt)->where('aid', $insertId)->count();
        if (empty($count)) {
            $r = Db::connect('dynamic')->name($tableExt)->insert($dataExt);
        } else {
            $r = Db::connect('dynamic')->name($tableExt)->where('aid', $insertId)->save($dataExt);
        }
        if(empty($r)){
            return null;
        }
        return $insertId;
    }

    public function updateProject($aid,$param){
        if(empty($aid) || empty($param)){
            return null;
        }
        $table = 'archives';
        $mod = Db::connect('dynamic')->name('channeltype')->field('id,ntitle,table')->find($this->web['cms_project']);
        $tableExt = $mod['table'].'_content';
        $r = Db::connect('dynamic')->name($tableExt)->where('aid',$aid)->update($param);
        return $r;
    }

    public function insertCompany($param){
        $table = 'archives';
        $mod = Db::connect('dynamic')->name('channeltype')->field('id,ntitle,table')->find($this->web['cms_company']);
        $tableExt = $mod['table'].'_content';
        $typeid = Db::connect('dynamic')->name('arctype')->where([
            ['channeltype','=',$mod['id']]
        ])->value('id');
        if(empty($typeid)){
            return null;
        }
        $count = Db::connect('dynamic')->name($table)->where([
            ['channel','=',$mod['id']],
            ['title','=',$param['title']]
        ])->count();
        if (!empty($count)) {
            return null;
        }
        $time = time();
        $data = [
            'typeid'      => empty($typeid) ? 0 : $typeid,
            'channel'     => $mod['id'],
            'title'       => $param['title'],
            'admin_id'    => 1,
            'sort_order'  => 100,
            'arcrank'     => -1,
            'click'       => mt_rand(100,999),
            'author'      => '小编',
            'add_time'    => $time,
        ];
        $insertId = Db::connect('dynamic')->name($table)->insertGetId($data);
        if(empty($insertId)){
            return null;
        }
        $dataExt = [
            'aid'            => $insertId,
            'add_time'       => $time,
            'update_time'    => $time,
            'companyName'    => $param['companyName'],
            'creditCode'     => $param['creditCode'],
            'companyCode'    => $param['companyCode'],
            'regNumber'      => $param['regNumber'],
            'taxNumber'      => $param['taxNumber'],
            'regType'        => $param['regType'],
            'regOrgName'     => $param['regOrgName'],
            'faRen'          => $param['faRen'],
            'companyType'    => $param['companyType'],
            'businessStatus' => $param['businessStatus'],
            'chkDate'        => $param['chkDate'],
            'issueTime'      => $param['issueTime'],
            'regDate'        => $param['regDate'],
            'bussiness'      => $param['bussiness'],
            'regMoney'       => $param['regMoney'],
            'address'        => $param['address'],
            'bussinessDes'   => $param['bussinessDes'],
            'phone'          => $param['phone'],
            'province'       => $param['province'],
            'city'           => $param['city'],
            'area'           => $param['area'],
            'industry'       => $param['industry'],
            'location'       => $param['location'],
        ];
        $count = Db::connect('dynamic')->name($tableExt)->where('aid', $insertId)->count();
        if (empty($count)) {
            $r = Db::connect('dynamic')->name($tableExt)->insert($dataExt);
        } else {
            $r = Db::connect('dynamic')->name($tableExt)->where('aid', $insertId)->save($dataExt);
        }
        if(empty($r)){
            return null;
        }
        return $insertId;
    }

    public function getCompany($title){
        $table = 'archives';
        $mod = Db::connect('dynamic')->name('channeltype')->field('id,ntitle,table')->find($this->web['cms_company']);
        $tableExt = $mod['table'].'_content';
        $aid = Db::connect('dynamic')->name($table)->where([
            ['channel','=',$mod['id']],
            ['title','=',$title]
        ])->value('aid');
        if(empty($aid)){
            return null;
        }
        $dataCom = Db::connect('dynamic')->name($tableExt)->field('id,address')->where('aid',$aid)->find();
        if(empty($dataCom)){
            return null;
        }
        return $dataCom;
    }

    public function insertArticle($param){
        $table = 'archives';
        $mod = Db::connect('dynamic')->name('channeltype')->field('id,ntitle,table')->find($this->web['cms_article']);
        $tableExt = $mod['table'].'_content';
        $typeid = Db::connect('dynamic')->name('arctype')->where([
            ['channeltype','=',$mod['id']],
            ['id','=',$param['catid']]
        ])->value('id');
        if(empty($typeid)){
            return null;
        }
        $time = time();
        $data = [
            'typeid'          => $typeid,
            'channel'         => $mod['id'],
            'title'           => $param['title'],
            'seo_keywords'    => $param['keywords'],
            'seo_description' => $param['description'],
            'admin_id'        => 1,
            'sort_order'      => 100,
            'arcrank'         => -1,
            'click'           => mt_rand(100,999),
            'author'          => '小编',
            'add_time'        => $time,
        ];

        $insertId = Db::connect('dynamic')->name($table)->insertGetId($data);
        if(empty($insertId)){
            return null;
        }
        $dataExt = [
            'aid'         => $insertId,
            'add_time'    => $time,
            'update_time' => $time,
            'content'     => $param['content'],
            'part'        => $param['part'],
            'attr'        => $param['attr'],
            'dalei'       => $param['dalei'],
            'xiaolei'     => $param['xiaolei'],
            'xiangmu'     => $param['project'],
        ];
        $count = Db::connect('dynamic')->name($tableExt)->where('aid', $insertId)->count();
        if (empty($count)) {
            $r = Db::connect('dynamic')->name($tableExt)->insert($dataExt);
        } else {
            $r = Db::connect('dynamic')->name($tableExt)->where('aid', $insertId)->save($dataExt);
        }
        if(empty($r)){
            return null;
        }

        http_request($this->web['url'].'?s=api/client/put_pic&arcid='.$insertId.'&modid='.$this->web['cms_article'],[
            'token:'.site_token(),
        ]);

        return $insertId;
    }


}